/**
 * @var YAK_AJAX
 */

layui.define(['jquery', 'layer'], function (exports) {
  var $ = layui.jquery
    , layer = layui.layer
    , Class = function (url, success, error, ant) {
      this.ant = ant ? ant : true;
      this.antIndex = 0;
      this.options.url = url;

      this.setResult(success, error)
      this.setBeforeSend();
      this.setError();
    }
    , YAK_AJAX = function (url, success, error) {
    return new Class(url, success, error)
  };

  Class.prototype.setResult = function (success, error) {

    if (success instanceof Function)
      this.success = success;

    if (error instanceof Function)
      this.error = error;

    var that = this;

    this.options.success = function (data) {
      if (that.ant === true) layer.close(that.antIndex);
      var code = data.code;
      switch (code) {
        case 302:
        case 403:
          window.location.href = data.redirect;
          break;
        case 0:
          that.success(data);
          break;
        default:
          that.error(data);
      }
    };
  };

  Class.prototype.setBeforeSend = function (callback) {
    var that = this;
    this.options.beforeSend = function (xhr) {
      xhr.setRequestHeader('AOP', 1);
      if (that.ant === true) {
        that.antIndex = layer.load();
      }
      if (callback instanceof Function) callback(xhr)
    }
  };

  Class.prototype.setError = function (callback) {
    var that = this;
    this.options.error = function (XMLHttpRequest, textStatus, errorThrown) {
      if (that.ant === true) {
        layer.close(that.antIndex);
      }
      if (callback instanceof Function)
        callback(XMLHttpRequest, textStatus, errorThrown)
      else layer.msg(errorThrown)
    };
  };

  Class.prototype.success = function (res) {
    layer.msg('操作成功')
  };

  Class.prototype.error = function (res) {
    layer.msg(res.msg, {icon: 5, anim: 6})
  };

  Class.prototype.setData = function (obj) {
    this.options.data = obj
  };

  Class.prototype.setDataValue = function (key, value) {
    this.options.data[key] = value
  };

  Class.prototype.setDataCondition = function (obj) {
    this.setDataValue('Condition', obj)
  }

  Class.prototype.setDataConditionValue = function (key, value) {
    var Condition = this.options.data.hasOwnProperty('Condition') ? this.options.data.Condition : {}
    Condition[key] = value;
    this.setDataValue('Condition', Condition)
  }

  Class.prototype.setDataData = function (obj) {
    this.setDataValue('Data', obj)
  }

  Class.prototype.setDataDataValue = function (key, value) {
    var Data = this.options.data.hasOwnProperty('Data') ? this.options.data.Data : {}
    Data[key] = value;
    this.setDataValue('Data', Data)
  }

  Class.prototype.setDataSort = function (obj) {
    this.setDataValue('Sort', obj)
  }

  Class.prototype.setDataSortValue = function (key, value) {
    var Sort = this.options.data.hasOwnProperty('Sort') ? this.options.data.Sort : {}
    Sort[key] = value;
    this.setDataValue('Sort', Sort)
  }

  Class.prototype.setMethod = function (method) {
    this.options.type = method
  };

  Class.prototype.setHeaders = function (obj) {
    this.options.headers = obj
  }

  Class.prototype.setHeadersValue = function (key, value) {
    this.options.headers[key] = value
  }

  Class.prototype.setOption = function (key, value) {
    this.options[key] = value;
  }

  Class.prototype.removeOption = function (key) {
    delete this.options[key];
  }

  Class.prototype.options = {
    url: "",
    type: "get",
    data: {},
    beforeSend: function (xhr) {
      xhr.setRequestHeader('AOP', 1);
    },
    headers: {},
    error: function (XMLHttpRequest, textStatus, errorThrown) {
    }
  }

  Class.prototype.send = function () {
    $.ajax(this.options)
  }

  exports('ax', YAK_AJAX)

})